﻿using System;
using System.Reflection;
using System.Data.SqlClient;

using Builderator.Core.Interfaces;

namespace Builderator.Core.Action
{
    public class ActionBase : Document, IDatabase, IAction, IDocument
    {
        [DocumentProperty("Identifier", typeof(Guid), true)]
        public Guid Identifier { get; set; }

        [DocumentProperty("Name", typeof(String), false)]
        public string Name { get; set; }

        [DocumentProperty("Type", typeof(Guid), true)]
        public Guid Type { get; set; }

        [DocumentProperty("Previous", typeof(Guid), true)]
        public Guid Previous { get; set; }

        [DocumentProperty("Next", typeof(Guid), true)]
        public Guid Next { get; set; }

        [DocumentProperty("Associated", typeof(Guid), true)]
        public Guid Associated { get; set; }

        public event DebugMessageDelegate DebugMessageEvent;

        public ActionBase(string name, Guid type)
            : base()
        {
            this.Identifier = Guid.NewGuid();

            this.Name = name;

            this.Type = type;

            this.Previous = Guid.Empty;

            this.Next = Guid.Empty;

            this.Associated = Guid.Empty;
        }

        public ActionBase(Guid type)
            : this(string.Empty, type)
        {

        }

        public void DebugMessage(string message)
        {
            if (this.DebugMessageEvent != null)
                this.DebugMessageEvent(message);
        }

        public virtual SqlCommand Select(DatabaseTable table)
        {
            SqlCommand command = new SqlCommand();

            command.CommandText = string.Format("SELECT * FROM actions WHERE identifier='{0}'", this.Identifier.ToString());

            return command;
        }

        public virtual SqlCommand Insert(DatabaseTable table)
        {
            SqlCommand command = new SqlCommand();

            command.CommandText = string.Format("INSERT INTO actions VALUES (@ID, @Name, @Type, @Prev, @Next, @Associated, @Settings)");

            command.Parameters.AddWithValue("@ID", this.Identifier.ToString());

            command.Parameters.AddWithValue("@Name", this.Name);

            command.Parameters.AddWithValue("@Type", this.Type.ToString());

            command.Parameters.AddWithValue("@Prev", this.Previous.ToString());

            command.Parameters.AddWithValue("@Next", this.Next.ToString());

            command.Parameters.AddWithValue("@Associated", this.Associated.ToString());

            command.Parameters.AddWithValue("@Settings", this.Serialize());

            return command;
        }

        public virtual SqlCommand Update(DatabaseTable table)
        {
            SqlCommand command = new SqlCommand();

            command.CommandText = string.Format("UPDATE actions SET previous='{0}', next='{1}', associated='{2}', settings='{3}' WHERE identifier='{4}'", this.Previous.ToString(), this.Next.ToString(), this.Associated.ToString(), this.Serialize(), this.Identifier.ToString());

            return command;
        }

        public virtual SqlCommand Delete(DatabaseTable table)
        {
            SqlCommand command = new SqlCommand();

            command.CommandText = string.Format("DELETE FROM actions WHERE identifier='{0}'", this.Identifier.ToString());

            return command;
        }
    }
}